/**
 * 
 */
package edu.hm.compiler.scanner.dfa.state;

import java.util.HashMap;
import java.util.Map;

import edu.hm.compiler.scanner.dfa.state.impl.ErrorState;
import edu.hm.compiler.scanner.dfa.symbol.ISymbol;

/**
 * @author Saruman
 */
public abstract class AbstractState implements IState
{

    private final Map< ISymbol, IState > transitions = new HashMap<>();
    private final int                    name;

    /**
     * @param name
     */
    public AbstractState( int name )
    {
        this.name = name;
    }

    @Override
    public IState getState( ISymbol symbol )
    {
        if( symbol.isEOF() )
            return new ErrorState();
        IState iState = this.transitions.get( symbol );
        if( iState == null )
            return new ErrorState();
        return iState;
    }

    @Override
    public IState addState( ISymbol symbol, IState state )
    {
        if( symbol == null || state == null )
            throw new IllegalArgumentException( "Arguments must not be null!" );
        this.transitions.put( symbol, state );
        return this;
    }

    @Override
    public int getName()
    {
        return this.name;
    }
}
